package com.fooddelivery;

import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ReviewFrame extends JFrame {
    private JComboBox<String> restaurantComboBox;
    private JSlider ratingSlider;
    private JTextArea commentArea;

    public ReviewFrame(int userId) {
        setTitle("Leave a Review");
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(3, 2));

        inputPanel.add(new JLabel("Restaurant:"));
        restaurantComboBox = new JComboBox<>();
        loadRestaurants();
        inputPanel.add(restaurantComboBox);

        inputPanel.add(new JLabel("Rating:"));
        ratingSlider = new JSlider(1, 5, 3);
        inputPanel.add(ratingSlider);

        inputPanel.add(new JLabel("Comment:"));
        commentArea = new JTextArea();
        inputPanel.add(new JScrollPane(commentArea));

        panel.add(inputPanel, BorderLayout.NORTH);

        JButton submitButton = new JButton("Submit Review");
        submitButton.addActionListener(e -> submitReview(userId));
        panel.add(submitButton, BorderLayout.SOUTH);

        add(panel);
    }

    private void loadRestaurants() {
        try (Connection conn = DatabaseConnection.getConnection()) {
            String sql = "SELECT * FROM restaurants";
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                restaurantComboBox.addItem(rs.getString("name"));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private void submitReview(int userId) {
        String restaurantName = (String) restaurantComboBox.getSelectedItem();
        int rating = ratingSlider.getValue();
        String comment = commentArea.getText();

        try (Connection conn = DatabaseConnection.getConnection()) {
            String sql = "INSERT INTO reviews (user_id, restaurant_id, rating, comment) VALUES (?, ?, ?, ?)";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, userId);
            stmt.setInt(2, getRestaurantId(restaurantName));
            stmt.setInt(3, rating);
            stmt.setString(4, comment);
            stmt.executeUpdate();

            JOptionPane.showMessageDialog(this, "Review submitted successfully!");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private int getRestaurantId(String name) throws SQLException {
        try (Connection conn = DatabaseConnection.getConnection()) {
            String sql = "SELECT id FROM restaurants WHERE name = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, name);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return rs.getInt("id");
            }
        }
        return -1;
    }
}
